package com.weijian.controller.admin;

import com.weijian.common.pojo.CommonResult;
import com.weijian.dal.vo.OrderReportVO;
import com.weijian.dal.vo.SalesTop10ReportVO;
import com.weijian.dal.vo.TurnoverReportVO;
import com.weijian.dal.vo.UserReportVO;
import com.weijian.service.ReportService;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.time.LocalDate;

@RestController()
@RequestMapping("/report")
@Slf4j
@RequiredArgsConstructor
public class ReportController {

    final ReportService reportService;

    /**
     * 营业额相关接口
     */
    @Schema(description = "营业额相关接口")
    @GetMapping("/turnoverStatistics")
    public CommonResult<TurnoverReportVO> turnoverStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("营业额统计:{},{}", begin, end);
        return reportService.getTurnoverStatistics(begin, end);
    }

    /**
     * 用户统计
     */
    @Schema(description = "用户统计")
    @GetMapping("/userStatistics")
    public CommonResult<UserReportVO> userStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("用户统计:{},{}", begin, end);
        return reportService.userStatistics(begin, end);
    }

    /**
     * 订单统计
     */
    @Schema(description = "订单统计")
    @GetMapping("/ordersStatistics")
    public CommonResult<OrderReportVO> orderStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("订单统计{},{}", begin, end);
        return reportService.orderStatistics(begin, end);
    }

    /**
     * 销量排名前十
     */
    @Schema(description = "销量排行前十")
    @GetMapping("/top10")
    public CommonResult<SalesTop10ReportVO> salesTop10Report(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("销售排名前10:{},{}", begin, end);
        return reportService.salesTop10Report(begin, end);
    }

    /**
     * 导出excel表格
     */
    @Schema(description = "导出excel表格")
    @GetMapping("/export")
    public void export(HttpServletResponse httpResponse) throws IOException {
        log.info("导出Excel表格");
        reportService.export(httpResponse);
    }
}
